package com.hcb.tonguetiphealth.manager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.hcb.tonguetiphealth.global.Global;

import android.util.Log;

public class JSONManager {
	private static final String TAG = "JSONOManager";
	private JSONArray jsonList = null;
	private String msg;
	private int result;
	private JSONObject jsonObject;

	/**
	 * results 封装为数组调用此方法
	 */
	public boolean isGotJSONObject(String response) {
		boolean succeed = false;
		try {
			JSONObject jo = new JSONObject(response) ;
			String strReturn = jo.getString("return").trim(); 
			if(strReturn.equals("true")) {
				result = getInt(jo, "result");
				jsonList = getJSONArray(jo, "results");
				succeed = true;
			}
			msg = jo.getString("msg"); 
			if(Global.DEBUG) {
				System.out.println("----- 服务端返回信息msg: " + msg);
			}
			 
		} catch (JSONException e) {
			Log.e(TAG, "isGotJSONObject", e);
		} catch (Exception e) {
			Log.e(TAG, "isGotJSONObject", e);
		}
		return succeed;
	}
	
	/**
	 * results 封装为对象时调用此方法
	 */
	public boolean isGotJSONObject2(String response) {
		boolean succeed = false;
		try {
			JSONObject jo = new JSONObject(response) ;
			String strReturn = jo.getString("return").trim(); 
			if(strReturn.equals("true")) {
				result = getInt(jo, "result");
				jsonObject = jo.getJSONObject("results");
				succeed = true;
			}
			msg = jo.getString("msg"); 
			if(Global.DEBUG) {
				System.out.println("----- 服务端返回信息msg: " + msg);
			}
			 
		} catch (JSONException e) {
			Log.e(TAG, "isGotJSONObject", e);
		} catch (Exception e) {
			Log.e(TAG, "isGotJSONObject", e);
		}
		return succeed;
	}
	
	private JSONArray getJSONArray(JSONObject jo, String value) {
		try {
			return jo.getJSONArray(value);
		} catch (JSONException e) {
			Log.e(TAG, "getJSONObject", e);
		} catch(Exception e) {
			Log.e(TAG, "getJSONObject", e);
		}
		return null;
	}
	private int getInt(JSONObject jo, String value) {
		try {
			return jo.getInt(value);
		} catch (JSONException e) {
			Log.e(TAG, "getJSONObject", e);
		} catch(Exception e) {
			Log.e(TAG, "getJSONObject", e);
		}
		return 0;
	}
 

	public String getMsg() {
		return msg==null? "":msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public int getResult() {
		return result;
	}

	public void setResult(int result) {
		this.result = result;
	}

	public JSONArray getJsonList() {
		return jsonList;
	}

	public void setJsonList(JSONArray jsonList) {
		this.jsonList = jsonList;
	}

	public JSONObject getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}
	
}
